<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      size="small"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="spuId" prop="spuId">
        <el-input v-model="dataForm.spuId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="成长积分" prop="growBounds">
        <el-input-number
          v-model="dataForm.growBounds"
          controls-position="right"
          :precision="2"
          :step="0.1"
          :min="0"
          label="成长积分"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="购物积分" prop="buyBounds">
        <el-input-number
          v-model="dataForm.buyBounds"
          controls-position="right"
          :precision="2"
          :step="0.1"
          :min="0"
          label="购物积分"
        ></el-input-number>
      </el-form-item>
      <!-- [1111（四个状态位，从右到左）;0 - 无优惠，成长积分是否赠送;1 - 无优惠，购物积分是否赠送;2 - 有优惠，成长积分是否赠送;3 - 有优惠，购物积分是否赠送【状态位0：不赠送，1：赠送】] -->
      <el-form-item label="优惠生效情况" prop="work">
        <el-input v-model="dataForm.work" placeholder="优惠生效情况"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveOrUpdateSpuBounds } from "@/api/coupon";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        spuId: "",
        growBounds: "",
        buyBounds: "",
        work: "",
      },
      dataRule: {
        spuId: [{ required: true, message: "不能为空", trigger: "blur" }],
        growBounds: [
          { required: true, message: "成长积分不能为空", trigger: "blur" },
        ],
        buyBounds: [
          { required: true, message: "购物积分不能为空", trigger: "blur" },
        ],
        work: [
          {
            required: true,
            message:
              "优惠生效情况[1111（四个状态位，从右到左）;0 - 无优惠，成长积分是否赠送;1 - 无优惠，购物积分是否赠送;2 - 有优惠，成长积分是否赠送;3 - 有优惠，购物积分是否赠送【状态位0：不赠送，1：赠送】]不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(data) {
      this.dataForm.id = 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (data) {
          this.dataForm.id = data.id;
          this.dataForm.spuId = data.spuId;
          this.dataForm.growBounds = data.growBounds;
          this.dataForm.buyBounds = data.buyBounds;
          this.dataForm.work = data.work;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          saveOrUpdateSpuBounds({
            id: this.dataForm.id || "",
            spuId: this.dataForm.spuId,
            growBounds: this.dataForm.growBounds,
            buyBounds: this.dataForm.buyBounds,
            work: this.dataForm.work,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
